#include "addcategory.h"
#include "ui_addcategory.h"
#include<QMessageBox>
#include<QDebug>
#include<QDesktopWidget>
#include<QtSql/QSqlQuery>
//This class allow either to add new cateogory or update existing
AddCategory::AddCategory(QString cat,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::AddCategory)
{
    ui->setupUi(this);
    catnm=cat;
    if(cat.compare("")!=0)
    {
      //  ui->label_2->setText(tr("Modify Category"));
        this->setWindowTitle(tr("Edit Category"));
    }
    else
    {
       // ui->label_2->setText(tr("Create new category"));
        this->setWindowTitle(tr("Add Category"));
    }

    QRect innersize=QApplication::desktop()->availableGeometry(this);
    ui->scrollArea->setGeometry(0,0,(innersize.width()),(innersize.height()));
    ui->lineEdit_name->setFocus();
    ui->verticalLayout->addSpacing(15);
    int fontSize=10;//(innersize.width()/30.0);
    ui->label->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));
    ui->verticalLayout->setStretch(0,0);
    ui->verticalLayout->setStretch(1,10);
   // ui->label_2->setFont(QFont( "lucida", (fontSize), QFont::Normal, FALSE ));
    ui->lineEdit_name->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));
    ui->pushButton->setFont(QFont( "lucida", (fontSize-2), QFont::Normal, FALSE ));
    QAction* backAction = new QAction( tr("Back"), this );
     backAction->setSoftKeyRole( QAction::NegativeSoftKey );
     connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
     addAction( backAction );
    ui->lineEdit_name->setText(catnm);
}

AddCategory::~AddCategory()
{
    delete ui;
}

void AddCategory::on_pushButton_clicked()
{
    QString name=ui->lineEdit_name->text();//Category name entered by user
    if(name.compare("")==0)
        QMessageBox::information(0,"Error !","Please enter the name of category !");
    else
    {
        if(db.db_connect())
        {
            if(catnm.compare("")==0)//No string means ,to add category
            {
            QSqlQuery qry;
            /*qry.prepare(QString("select * from category_table where category_name='%1'").arg(name));
            if(qry.exec()&& (qry.size()> -1))
            {
               QMessageBox::information(0,"Error !","Please enter another name for category !");

            }
            else
            {}*/
                if(db.db_category_insert(name))
            {
                    QMessageBox::information(0,"Success !","Category added successfully !");
                    (new CatSelect)->show();
                }
                else
                    QMessageBox::information(0,"Error !","Error in adding category,please enter another name !");


            }
            else//Some string means, to edit category
            {
                if(catnm.compare(name,Qt::CaseInsensitive)==0)//If entered name is same as that of selected
                    QMessageBox::warning(0,"Warning !","Both names are same, please enter another name !");
                else
                {
                if(db.db_category_update(catnm,name))
                    {
                    QMessageBox::information(0,"Success !","Category updated successfully !");
                    (new CatSelect)->show();
                }
                else
                    QMessageBox::information(0,"Error !","Error in updating category,please enter another name !");
                }
            }
            db.db_close();
        }//connect
    }//text field
}
void AddCategory::back()
{
    (new CatSelect)->show();
}
